package com.example.ShoppingManagementSystem.control;

import com.example.ShoppingManagementSystem.entity.Admin;
import com.example.ShoppingManagementSystem.entity.Customer;
import com.example.ShoppingManagementSystem.entity.Product;
import com.example.ShoppingManagementSystem.service.AdminServicempl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/AdminService")
public class AdminControl {

    private final AdminServicempl adminService;
    @Autowired
    public AdminControl(AdminServicempl adminService) {
        this.adminService = adminService;
    }

    @PostMapping("/loginAdmin")
    public ResponseEntity<Admin> loginAdmin(@RequestParam String username, @RequestParam String password) {
        try {
            System.out.println("loginAdmin");
            Admin admin = adminService.loginAdmin(username, password);
            if (admin != null) {
                return ResponseEntity.ok(admin);
            } else {
                return ResponseEntity.status(401).body(null); // Unauthorized
            }
        }
        catch (Exception e) {
            System.out.println("service warning");
            return ResponseEntity.status(401).body(null);
        }
    }

    @PostMapping("/updatePassword")
    public ResponseEntity<String> updatePassword( @RequestParam String newPassword) {
        try {
            adminService.updatePassword( newPassword);
            return ResponseEntity.ok("Password updated successfully.");
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage()); // Not Found
        }
    }

    @PostMapping("/changeCustomerPassword")
    public ResponseEntity<String> changeCustomerPassword(@RequestParam String username, @RequestParam String newPassword) {
        try {
            adminService.changeCustomerPassword(username, newPassword);
            return ResponseEntity.ok("Customer password updated successfully.");
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage()); // Not Found
        }
    }

    @GetMapping("/getAllCustomers")
    public ResponseEntity<List<Customer>> getAllCustomers() {
        List<Customer> customers = adminService.getAllCustomers();
        return ResponseEntity.ok(customers);
    }

    @DeleteMapping("/delCustomer")
    public ResponseEntity<String> deleteCustomer(@RequestParam int customerId) {
        try {
            adminService.delCustomer(customerId);
            return ResponseEntity.ok("Customer deleted successfully.");
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage()); // Not Found
        }
    }

    @GetMapping("/findCustomerByUsername")
    public ResponseEntity<Customer> findCustomerByUsername(@RequestParam String username) {
        try {
            Customer customer = adminService.findCustomerByUsername(username);
            return ResponseEntity.ok(customer);
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(null); // Not Found
        }
    }

    @GetMapping("/findCustomerByID")
    public ResponseEntity<Customer> findCustomerByID(@RequestParam int id) {
        try {
            Customer customer = adminService.findCustomerByID(id);
            return ResponseEntity.ok(customer);
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(null); // Not Found
        }
    }

    @GetMapping("/getAllProduct")
    public ResponseEntity<List<Product>> getAllProducts() {
        List<Product> products = adminService.getAllProduct();
        return ResponseEntity.ok(products);
    }

    @PostMapping("/addProduct")
    public ResponseEntity<String> addProduct(@RequestBody ProductRequest productRequest) {
        System.out.println("Received product: " + productRequest);
        try {
            adminService.addProduct(
                    productRequest.getProductName(),
                    productRequest.getManufacturer(),
                    productRequest.getModel(),
                    productRequest.getPurchasePrice(),
                    productRequest.getRetailPrice(),
                    productRequest.getQuantity()
            );
            return ResponseEntity.ok("Product added successfully.");
        } catch (RuntimeException e) {
            return ResponseEntity.status(409).body(e.getMessage()); // Conflict
        }
    }

    @PutMapping("/updateProduct")
    public ResponseEntity<String> updateProduct(@RequestBody Product product) {
        try {
            adminService.updateProduct(product);
            return ResponseEntity.ok("Product updated successfully.");
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage()); // Not Found
        }
    }

    @DeleteMapping("/deleteProduct")
    public ResponseEntity<String> deleteProduct(@RequestParam int id) {
        try {
            adminService.deleteProduct(id);
            return ResponseEntity.ok("Product deleted successfully.");
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage()); // Not Found
        }
    }
}
